package com.zqxq.lottery.common.utils;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 说    明： 组合工具类
 * 创 建 人：	一刀·胡
 * 创建时间：	2018/5/15 13:42
 * 修 改 人：
 * 修改日期：
 **/
public class Combination<T> {
    private Class<T> type;
    private T[] data;

    public Combination(Class clazz) {
        type = clazz;
    }

    public Combination(T[] data) {
        this.data = data;
        type = (Class<T>) data.getClass().getComponentType();
    }

    public static long count(int n, int m) {
        return  combine(n,m);
    }

    public <T> T[] newArray( int newLength) {
        return (T[]) Array.newInstance(type, newLength);
    }

    //组合 select 方案
    public List<T[]> select(int m) {
        List<T[]> result = new ArrayList((int)count(this.data.length, m));
        this.select(0, newArray(m), 0, result);
        return result;
    }

    public void select(int dataIndex, T[] resultList, int resultIndex, List<T[]> result) {
        int resultLen = resultList.length;
        int resultCount = resultIndex + 1;
        if (resultCount > resultLen) {
            result.add(Arrays.copyOf(resultList, resultList.length));
        } else {
            for(int i = dataIndex; i < this.data.length + resultCount - resultLen; ++i) {
                resultList[resultIndex] = this.data[i];
                this.select(i + 1, resultList, resultIndex + 1, result);
            }
        }
    }

    // 组合(C 下标m,上标n)
    public static long combine(int m, int n) {
        if (m < n || n < 0) {
            return 0L;
        }
        return factorial(m, m - n + 1) / factorial(n, 1);
    }

    // 阶乘
    public static long factorial(long max, long min) {
        if (max >= min && max > 1) {
            return max * factorial(max - 1, min);
        } else {
            return 1L;
        }
    }

    //排列
    public static long permutation(long max, long min) {
        if (max >= min && min > 0) {
            return max * permutation(max - 1, min - 1);
        } else {
            return 1L;
        }
    }

    //多维数组笛卡儿积
    public void selectCP(List<T[]> result,T[][] source,int layer,T[] val){
        if(layer==0)
            val = (T[]) Array.newInstance(type, source.length);
        if(layer == source.length-1){
            for(T a: source[layer]){
                val[layer] = a;
                result.add(Arrays.copyOf(val, val.length));
            }
            return;
        }else{
            for(T a: source[layer]){
                val[layer] = a;
                selectCP(result,source,layer+1,Arrays.copyOf(val, val.length));
            }
        }
    }

    public static void clacCM(List<String> list,String[][] arr,int index){
        if(index == arr.length-1){
            for(String a: arr[index]){
                list.add(a);
            }
            return;
        }else{
            for(String a: arr[index]){
                clacCM(list,arr,index+1);
            }
        }
    }

    public static void main(String [] args){
        String[][] arr = new String[4][];
        arr[0] = new String[]{"周一001|胜|1","周一001|平|2"};
        arr[1] = new String[]{"周二001|胜|3","周二001|平|4"};
        arr[2] = new String[]{"周三001|胜|5"};
        arr[3] = new String[]{"周四001|胜|6"};
        Combination cb = new Combination(arr);
        String[] betBunchs = "2,3,4".split(",");
        for(String betBunch : betBunchs){
            List<String[][]> cm = cb.select(Integer.valueOf(betBunch));
            for(String[][] c : cm){

            }
            System.out.println(cm.toString());
        }
        System.out.println("组合："+1);
    }
}
